package com.UKIT.airtickets.Service.Handlers

import TicketAdapter
import android.content.Context
import android.widget.TextView
import com.UKIT.airtickets.DB.AppDatabase
import com.UKIT.airtickets.DB.UserFavorite
import com.UKIT.airtickets.Service.SettingsApp
import com.UKIT.airtickets.Service.Tikets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class FavouritesHandler(private val context: Context) {

    suspend fun updateFavoritesList(adapter: TicketAdapter, total: TextView? = null) {
        val ticketsList = getFavoritesFromDatabase()
        if(total != null){
            var sum = 0
            ticketsList.forEach{
                sum+=it.price
            }
            total.text = StringBuilder("Сумма: ${sum}")
            adapter.setData(ticketsList)
        }
    }

    suspend fun toggleFavoriteStatus(ticket: Tikets, adapter: TicketAdapter) {
        if (isInFavorites(ticket)) {
            removeTicketFromFavorites(ticket)
        } else {
            addTicketToFavorites(ticket)
        }
        updateFavoritesList(adapter)
    }

    suspend fun getFavoritesFromDatabase(): List<Tikets> {
        val userFavoriteDao = AppDatabase.getDatabase(context).userFavoriteDao()
        return withContext(Dispatchers.IO) {
            val favorites = userFavoriteDao.getUserFavorites(SettingsApp.userId.toLong())
            favorites.map { favorite ->
                Tikets(
                    searchToken = favorite.searchToken,
                    startCity = favorite.startCity,
                    startCityCode = favorite.startCityCode,
                    endCity = favorite.endCity,
                    endCityCode = favorite.endCityCode,
                    startDate = favorite.startDate,
                    endDate = favorite.endDate,
                    price = favorite.price
                )
            }
        }
    }

    private suspend fun isInFavorites(ticket: Tikets): Boolean {
        val searchToken = ticket.searchToken
        val userFavoriteDao = AppDatabase.getDatabase(context).userFavoriteDao()
        return withContext(Dispatchers.IO) {
            userFavoriteDao.isFavorite(searchToken, SettingsApp.userId.toLong()) > 0
        }
    }

    private suspend fun addTicketToFavorites(ticket: Tikets) {
        val userId = SettingsApp.userId.toLong()
        val userFavoriteDao = AppDatabase.getDatabase(context).userFavoriteDao()
        withContext(Dispatchers.IO) {
            val userFavorite = UserFavorite(
                userId = userId,
                searchToken = ticket.searchToken,
                startCity = ticket.startCity,
                startCityCode = ticket.startCityCode,
                endCity = ticket.endCity,
                endCityCode = ticket.endCityCode,
                startDate = ticket.startDate,
                endDate = ticket.endDate,
                price = ticket.price
            )
            userFavoriteDao.insertUserFavorite(userFavorite)
        }
    }

    private suspend fun removeTicketFromFavorites(ticket: Tikets) {
        val searchToken = ticket.searchToken
        val userFavoriteDao = AppDatabase.getDatabase(context).userFavoriteDao()
        withContext(Dispatchers.IO) {
            userFavoriteDao.deleteFavorite(searchToken)
        }
    }
}
